package com.spring.demo.rabbit.config;

import com.spring.demo.rabbit.ExchangeConst;
import com.spring.demo.rabbit.QueueConst;
import com.spring.demo.rabbit.RoutingKeyConst;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/***
 * @title DirectExchangeConfig
 * @description
 * @author baiji
 * @version 1.0.0
 * @create 2023/9/4 11:13
 **/
@Configuration
public class DirectExchangeConfig {

    @Resource
    private AmqpAdmin amqpAdmin;

    /**
     * direct 交换器 直接绑定
     *
     * @return
     */
    @Bean
    public DirectExchange directExchange() {
        DirectExchange exchange = ExchangeBuilder.directExchange(ExchangeConst.DIRECT_EXCHANGE).build();
        amqpAdmin.declareExchange(exchange);
        Queue direct_queue_01 = QueueBuilder.durable(QueueConst.DIRECT_QUEUE_01).build();
        amqpAdmin.declareQueue(direct_queue_01);
        Queue direct_queue_02 = QueueBuilder.durable(QueueConst.DIRECT_QUEUE_02).build();
        amqpAdmin.declareQueue(direct_queue_02);
        BindingBuilder.bind(direct_queue_01).to(exchange).with(RoutingKeyConst.DIRECT_ROUTING_KEY_01);
        BindingBuilder.bind(direct_queue_02).to(exchange).with(RoutingKeyConst.DIRECT_ROUTING_KEY_02);
        return exchange;
    }
}
